package com.shanshan.system.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.shanshan.system.easyexcel.DiseaseExcel;
import com.shanshan.system.service.RecordService;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * excel控制器
 *
 * @author 陈灰灰
 * @date 2022/10/24
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private RecordService recordService;


    @RequestMapping("/download")
    public void download(HttpServletResponse response ,int reservationId ) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("提交记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        DiseaseExcel diseaseExcel = recordService.recordService(reservationId);
        List<DiseaseExcel> data = new ArrayList<>();
        data.add(diseaseExcel);
        System.out.println(data);
        EasyExcel.write(response.getOutputStream(), DiseaseExcel.class).sheet("提交信息").doWrite(data);
    }

//    @GetMapping("/book/export")
//    public void export(HttpServletResponse response,int reservationId) throws IOException {
//        //获取需要导出的数据
//        DiseaseExcel diseaseExcel = recordService.recordService(reservationId);
//        List<DiseaseExcel> data = new ArrayList<>();
//        data.add(diseaseExcel);
//        List<DiseaseExcel> dataList = data;
//        //excel文件名
//        final String FILENAME = "提交信息信息";
//        //sheetName
//        final String SHEETNAME = "三会一课信息表";
//        //获取model对象类
//        Class book = Book.class;
//
//        try {
//            //表头样式策略
//            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//            //设置头居中
//            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//            //内容策略
//            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//            //设置 水平居中
//            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
//            //初始化表格样式
//            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//            response.setCharacterEncoding("utf-8");
//            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//            String fileName = URLEncoder.encode(FILENAME, "UTF-8").replaceAll("\\+", "%20");
//            //响应首部 Access-Control-Expose-Headers 就是控制“暴露”的开关，它列出了哪些首部可以作为响应的一部分暴露给外部。
//            //此处设置了开放Content-Disposition，前端可获取该响应参数获取文件名称
//            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//            // 这里需要设置不关闭流
//            EasyExcel.write(response.getOutputStream(), book).autoCloseStream(Boolean.FALSE)
//                    .registerWriteHandler(horizontalCellStyleStrategy).sheet(SHEETNAME).doWrite(dataList);
//
//        } catch (IOException e) { //下载失败情况的处理
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            Map<String, String> map = MapUtils.newHashMap();
//            map.put("status", "failure");
//            map.put("message", "下载文件失败" + e.getMessage());
//            response.getWriter().println(JSON.toJSONString(map));
//        }
//    }


}
